﻿/*
 * *******************************************************
 *
 * 作者：hzy
 *
 * 开源地址：https://gitee.com/hzy6
 *
 * *******************************************************
 */

using HzyScanDiService.Impl;
using HzyScanDiService.Interface;
using HzyScanDiService.Models;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace HzyScanDiService
{

    /// <summary>
    /// 扫描服务
    /// </summary>
    public class DiServiceScanningProvider
    {
        /// <summary>
        /// 开始扫描
        /// </summary>
        public void ScanningDiInterface(IServiceCollection serviceCollection, IEnumerable<Assembly> assemblies)
        {
            #region Interface 方式 自动注册

            serviceCollection.Scan(w =>
            {
                w.FromAssemblies(assemblies)
                    //接口注册Scoped
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiScoped)))
                        .AsImplementedInterfaces()
                        .WithScopedLifetime()
                    //接口注册Singleton
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiSingleton)))
                          .AsImplementedInterfaces()
                          .WithSingletonLifetime()
                    //接口注册Transient
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiTransient)))
                          .AsImplementedInterfaces()
                          .WithTransientLifetime()
                    //具体类注册Scoped
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiScopedSelf)))
                          .AsSelf()
                          .WithScopedLifetime()
                    //具体类注册Singleton
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiSingletonSelf)))
                          .AsSelf()
                          .WithSingletonLifetime()
                    //具体类注册Transient
                    .AddClasses(classes => classes.AssignableTo(typeof(IDiTransientSelf)))
                          .AsSelf()
                          .WithTransientLifetime();
            });

            #endregion
        }

        /// <summary>
        /// 扫描特性 class
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="serviceType"></param>
        /// <exception cref="NotSupportedException"></exception>
        public void ScanningAopBaseAttribute(IServiceCollection serviceCollection, Type serviceType)
        {
            var classContext = new ClassContext(serviceType);
            IDynamicProxy dynamicProxy = new DynamicProxyImpl();
            //
            var aopBaseAttribute = classContext.GetAopBaseAttribute();
            if (aopBaseAttribute == null) return;
            //生成动态实现类
            var serviceImplType = dynamicProxy.GenerateDynamicProxy(classContext).GetType();

            Type type = aopBaseAttribute.GetDiServiceTypeInfo();

            if (type == null)
            {
                type = serviceType;
            }

            //注册服务
            switch (aopBaseAttribute.GetDiServiceType())
            {
                case DiServiceTypeEnum.Scoped:
                    serviceCollection.AddScoped(type, serviceImplType);
                    break;
                case DiServiceTypeEnum.Transient:
                    serviceCollection.AddTransient(type, serviceImplType);
                    break;
                case DiServiceTypeEnum.Singleton:
                    serviceCollection.AddSingleton(type, serviceImplType);
                    break;
                default:
                    throw new NotSupportedException();
                    //break;
            }
        }







    }

}
